{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Welcome to the Second Lab - Week 1, Day 3\n",
    "\n",
    "This program uses Evaluator Optimizer pattern to enhance generator's response in creating marketing content for smart keyboard."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Start with imports - ask ChatGPT to explain any package that you don't know\n",
    "\n",
    "import os\n",
    "import json\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "from anthropic import Anthropic\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Always remember to do this!\n",
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Print the key prefixes to help with any debugging\n",
    "\n",
    "openai_api_key = os.getenv('OPENAI_API_KEY')\n",
    "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
    "\n",
    "if openai_api_key:\n",
    "    print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
    "else:\n",
    "    print(\"OpenAI API Key not set\")\n",
    "    \n",
    "if anthropic_api_key:\n",
    "    print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
    "else:\n",
    "    print(\"Anthropic API Key not set (and this is optional)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "request = \"Provide a short marketing content for XYZ keyboard. \"\n",
    "request += \"It should be eagaging and talks about innovative features.\"\n",
    "messages = [{\"role\": \"user\", \"content\": request}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "openai = OpenAI()\n",
    "\n",
    "response = openai.chat.completions.create(\n",
    "    model=\"gpt-4o-mini\",\n",
    "    messages=messages,\n",
    ")\n",
    "marketing_statement= response.choices[0].message.content\n",
    "print(marketing_statement)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "judge = f\"\"\"### Instruction ###\n",
    "You are an expert tech gadget analyst. Your task is to evaluate a marketing material based on several criteria.\n",
    "Please be brief.\n",
    "\n",
    "### Ad to Evaluate ###\n",
    "{marketing_statement}\n",
    "\n",
    "### Evaluation Criteria ###\n",
    "Evaluate the statement based on how engaging it is.\n",
    "\n",
    "### Expected Output Format ###\n",
    "Respond with JSON, and only JSON, with the following format:\n",
    "{{\"results\": {{\"statement\": \"{marketing_statement}\", \"engagability\": \"Comment on whether the content is engaging\", \"critique\": \"Offer a specific critique and suggest at least one way the recipe could be improved\", \"verdict\": \"This should have a value either 'accepted' or 'rejected' based on whether the statement requires improvement\"}}}}\n",
    "\"\"\"\n",
    "\n",
    "print(judge)\n",
    "judge_messages = [{\"role\": \"user\", \"content\": judge}]\n",
    "\n",
    "model_name = \"claude-3-7-sonnet-latest\"\n",
    "claude = Anthropic()\n",
    "response = claude.messages.create(model=model_name, messages=judge_messages, max_tokens=1000)\n",
    "marketing_statement_feedback = response.content[0].text\n",
    "\n",
    "print(marketing_statement_feedback)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_dict = json.loads(marketing_statement_feedback)\n",
    "feedback = results_dict[\"results\"]\n",
    "print(feedback)\n",
    "print(\"\\n\\n\")\n",
    "display(Markdown(marketing_statement_feedback))\n",
    "\n",
    "print(f\"Marketing statement:\\n{feedback[\"statement\"]}\")\n",
    "for key in feedback:\n",
    "    if key == \"verdict\":\n",
    "        if feedback[key] == \"accepted\":\n",
    "            print(\"Marketing statement was accepted.\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"Marketing statement was rejected and requires revision. Please iterate over to call Generator and Evaluator for improvement\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
